<!DOCTYPE html>





<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.9.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.ico?v=7.4.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico?v=7.4.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico?v=7.4.0">
  <link rel="mask-icon" href="/images/logo.svg?v=7.4.0" color="#222">

<link rel="stylesheet" href="/css/main.css?v=7.4.0">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.4.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: 'search.json',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    },
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="什么是rebaseRebase对于很多人来说是一个很抽象的概念，也因此它的学习门槛就在于如何了解这个抽象的概念。对于rebase 比较恰当的比喻应该是「移花接木」，简单来讲把你的分支接到别的分支上，稍后我们用几个图来示范merge与rebase 的差异。 了解rebase之前，我们必须了解什么是base。对Git的使用者而言，在分支中进行开发活动是稀松平常的事情，也因此在合并管理分支时，也就需要了">
<meta name="keywords" content="linux,git">
<meta property="og:type" content="article">
<meta property="og:title" content="不会rebase就等于没学过Git">
<meta property="og:url" content="https://unix2dos.github.io/p/b1718ace.html">
<meta property="og:site_name" content="Levon&#39;s Blog">
<meta property="og:description" content="什么是rebaseRebase对于很多人来说是一个很抽象的概念，也因此它的学习门槛就在于如何了解这个抽象的概念。对于rebase 比较恰当的比喻应该是「移花接木」，简单来讲把你的分支接到别的分支上，稍后我们用几个图来示范merge与rebase 的差异。 了解rebase之前，我们必须了解什么是base。对Git的使用者而言，在分支中进行开发活动是稀松平常的事情，也因此在合并管理分支时，也就需要了">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://unix2dos.github.io/p/b1718ace/1.png">
<meta property="og:image" content="https://unix2dos.github.io/p/b1718ace/2.png">
<meta property="og:image" content="https://unix2dos.github.io/p/b1718ace/3.png">
<meta property="og:image" content="https://unix2dos.github.io/p/b1718ace/4.png">
<meta property="og:image" content="https://unix2dos.github.io/p/b1718ace/5.png">
<meta property="og:image" content="https://unix2dos.github.io/p/b1718ace/6.png">
<meta property="og:updated_time" content="2019-09-27T15:36:51.136Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="不会rebase就等于没学过Git">
<meta name="twitter:description" content="什么是rebaseRebase对于很多人来说是一个很抽象的概念，也因此它的学习门槛就在于如何了解这个抽象的概念。对于rebase 比较恰当的比喻应该是「移花接木」，简单来讲把你的分支接到别的分支上，稍后我们用几个图来示范merge与rebase 的差异。 了解rebase之前，我们必须了解什么是base。对Git的使用者而言，在分支中进行开发活动是稀松平常的事情，也因此在合并管理分支时，也就需要了">
<meta name="twitter:image" content="https://unix2dos.github.io/p/b1718ace/1.png">
  <link rel="canonical" href="https://unix2dos.github.io/p/b1718ace">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>不会rebase就等于没学过Git | Levon's Blog</title>
  








  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">
  <div class="container use-motion">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Levon's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <h1 class="site-subtitle" itemprop="description">微信:  L6241425</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>时光</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-categories">
      
    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-tags">
      
    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger">
        
          <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
      </li>
    
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="none"
           placeholder="搜索..." spellcheck="false"
           type="text" id="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
            

          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
      <article itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block post">
    <link itemprop="mainEntityOfPage" href="https://unix2dos.github.io/p/b1718ace.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Levon">
      <meta itemprop="description" content="Never Give Up">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Levon's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">不会rebase就等于没学过Git

          
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2018-07-07 12:40:10" itemprop="dateCreated datePublished" datetime="2018-07-07T12:40:10+08:00">2018-07-07</time>
            </span>
          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-09-27 23:36:51" itemprop="dateModified" datetime="2019-09-27T23:36:51+08:00">2019-09-27</time>
              </span>
            
          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/2-linux系统/" itemprop="url" rel="index"><span itemprop="name">2-linux系统</span></a></span>

                
                
                  ，
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/2-linux系统/git/" itemprop="url" rel="index"><span itemprop="name">git</span></a></span>

                
                
              
            </span>
          

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
        
      
      <span class="post-meta-item-text">Disqus：</span>
    
    <a title="disqus" href="/p/b1718ace.html#comments" itemprop="discussionUrl"><span class="post-comments-count disqus-comment-count" data-disqus-identifier="p/b1718ace.html" itemprop="commentCount"></span></a>
  </span>
  
  
          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
              
                <span class="post-meta-item-text">本文字数：</span>
              
              <span>9.4k</span>
            </span>
          
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
              
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              
              <span>9 分钟</span>
            </span>
          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="什么是rebase"><a href="#什么是rebase" class="headerlink" title="什么是rebase"></a>什么是rebase</h2><p>Rebase对于很多人来说是一个很抽象的概念，也因此它的学习门槛就在于如何了解这个抽象的概念。对于rebase 比较恰当的比喻应该是「移花接木」，简单来讲把你的分支接到别的分支上，稍后我们用几个图来示范merge与rebase 的差异。</p>
<p>了解rebase之前，我们必须了解什么是base。对Git的使用者而言，在分支中进行开发活动是稀松平常的事情，也因此在合并管理分支时，也就需要了解分支是在哪个时间点哪个提交点分出来的旁支，而长出旁支来的提交点，对于旁支来说就是base commit，也就是base。所以简单来说，rebase其实就是改变分支的base的功能。</p>
<p>下图是在merge的情况会产生的版本演进的示意图，可以看到在新的分支中所做的变更，在合并之后，一并成为一个新的提交(commit 6)。而commit 1就是New Branch的base。</p>
<p><img src="/p/b1718ace/1.png" alt="1"></p>
<a id="more"></a>

<p>而下图是rebase 的情况下会产生的版本演进的示意图。我们同样是在分支中进行开发的动作，但是在rebase时，与merge不同的是，Git会将分支上所做的变更先暂存起来，接着把newbase (或称新基准点)合并进来，最后直接将刚刚暂存起来的变更在分支上重演，这边用「重演」这个字眼是表示「<strong>rebase不是将提交(commit)复制到分支上，而是将整个变更过程一个一个重新套用到分支上</strong>」 ，也就因为如此commit 2’与commit 3’，才会有另外的’符号表示与原本的commit 2 , commit 3不同，这点可以从commit的SHA1凑值不同看出来，虽然变更的内容相同，但是commit编号是不同的。本文会在稍后利用范例演示一遍。</p>
<p><img src="/p/b1718ace/2.png" alt="1"></p>
<p>也就因为如此，所以rebase的行为就很像「移花接木」，以上图来说，就是把New Branch的变更整个接到Master上。这样的好处就是 commit 更像一条直线,更优雅.</p>
<h2 id="Rebase-基础用法"><a href="#Rebase-基础用法" class="headerlink" title="Rebase -基础用法"></a>Rebase -基础用法</h2><p>以下我们用一个情境示范rebase的「基础用法」：</p>
<blockquote>
<p>你是一位team leader，你的其中一项职务就是负责进行程式码审查(code review)，并且将不同程式分支进行合并管理。</p>
<p>现在有2位程式设计师以develop分支为基础，分别开了新的分支feature-a与feature-b，也都已经完工了。你希望利用rebase的方式将这2个分支并入develop中。</p>
</blockquote>
<p> 首先，develop的日志如下所示：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">commit 38844ba14312c642dcd0f72baf031de0c50ad736</span><br><span class="line">Author: one.man.army &lt;one.man.army@example.com&gt;</span><br><span class="line">Date: Mon Sep 22 15:41:04 2014 +0800</span><br><span class="line"></span><br><span class="line">    add HelloWorld.c</span><br><span class="line"></span><br><span class="line">commit 3908e6bc1007f12566fdb5a0fb43f4055560b880</span><br><span class="line">Author: one.man.army &lt;one.man.army@example.com&gt;</span><br><span class="line">Date: Mon Sep 22 15:40:42 2014 +0800</span><br><span class="line"></span><br><span class="line">    initial commit</span><br></pre></td></tr></table></figure>

<p>接着，feature-a的日志如下所示:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">commit 15bf9c8954633700211f5b9d246ae67d8135cf29</span><br><span class="line">Author: one.man.army &lt;one.man.army@example.com&gt;</span><br><span class="line">Date: Mon Sep 22 15:42:48 2014 +0800</span><br><span class="line"></span><br><span class="line">    add feature_a.c</span><br><span class="line"></span><br><span class="line">commit 38844ba14312c642dcd0f72baf031de0c50ad736</span><br><span class="line">Author: one.man.army &lt;one.man.army@example.com&gt;</span><br><span class="line">Date: Mon Sep 22 15:41:04 2014 +0800</span><br><span class="line"></span><br><span class="line">    add HelloWorld.c</span><br><span class="line"></span><br><span class="line">commit 3908e6bc1007f12566fdb5a0fb43f4055560b880</span><br><span class="line">Author: one.man.army &lt;one.man.army@example.com&gt;</span><br><span class="line">Date: Mon Sep 22 15:40:42 2014 +0800</span><br><span class="line"></span><br><span class="line">    initial commit</span><br></pre></td></tr></table></figure>

<p>最后是feature-b的日志：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">commit e9d7a6f8b27bca86ef298911d84891b8a7efeada</span><br><span class="line">Author: one.man.army &lt;one.man.army@example.com&gt;</span><br><span class="line">Date: Mon Sep 22 15:45:37 2014 +0800</span><br><span class="line"></span><br><span class="line">    add #include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">commit eb6436b59b7a0624f3ec5e5469ac36b37b5211e7</span><br><span class="line">Author: one.man.army &lt;one.man.army@example.com&gt;</span><br><span class="line">Date: Mon Sep 22 15:43:55 2014 +0800</span><br><span class="line"></span><br><span class="line">    add feature_b.c</span><br><span class="line"></span><br><span class="line">commit 38844ba14312c642dcd0f72baf031de0c50ad736</span><br><span class="line">Author: one.man.army &lt;one.man.army@example.com&gt;</span><br><span class="line">Date: Mon Sep 22 15:41:04 2014 +0800</span><br><span class="line"></span><br><span class="line">    add HelloWorld.c</span><br><span class="line"></span><br><span class="line">commit 3908e6bc1007f12566fdb5a0fb43f4055560b880</span><br><span class="line">Author: one.man.army &lt;one.man.army@example.com&gt;</span><br><span class="line">Date: Mon Sep 22 15:40:42 2014 +0800</span><br><span class="line"></span><br><span class="line">    initial commit</span><br></pre></td></tr></table></figure>

<p>可以看到feature-a与feature-b分别比develop多出了1, 2个提交。</p>
<p>身为一名专业的team leader，我们有着足够的信心，相信这2个分支运作的很好，因此我们用以下指令进行rebase。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout develop</span><br><span class="line">$</span><br><span class="line">$ git rebase feature-a</span><br><span class="line">First, rewinding head to replay your work on top of it...</span><br><span class="line">Fast-forwarded develop to feature-a.</span><br><span class="line">$</span><br><span class="line">$ git rebase feature-b</span><br><span class="line">First, rewinding head to replay your work on top of it...</span><br><span class="line">Applying: add feature_a.c</span><br></pre></td></tr></table></figure>

<p>在上述指令中，我们先切换到develop分支中，接着我们很快的就利用指令git rebase <newbase>合并了feature-a与feature-b。此外，在上述的指令执行结果中，可以看到一行讯息显示Fast-forwarded develop to feature-a，其中的Fast-forwarded是什么意思呢？</newbase></p>
<blockquote>
<p>Fast-forwarded指的就是当2个分支的头尾相接时，代表2者之间不会有conflict ，因此只要改HEAD的指向就能够迅速合并了。以本情境为例，develop的最后一个提交正好是feature-a的头，所以这两者的rebase适用Fast-forwarded模式。</p>
</blockquote>
<p>接下来，可以用git log看看develop的日志，我们可以从日志中发现feature-a与feature-b的commit ID都不一样了。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">$ git log</span><br><span class="line">commit 07ef0b8e0b1edd079fb8b69f6e6e215725b5aba4</span><br><span class="line">Author: spitfire-sidra &lt;spitfire.sidra@gmail.com&gt;</span><br><span class="line">Date: Mon Sep 22 15:42:48 2014 +0800</span><br><span class="line"></span><br><span class="line">    add feature_a.c</span><br><span class="line"></span><br><span class="line">commit e9d7a6f8b27bca86ef298911d84891b8a7efeada</span><br><span class="line">Author: spitfire-sidra &lt;spitfire.sidra@gmail.com&gt;</span><br><span class="line">Date: Mon Sep 22 15:45:37 2014 +0800</span><br><span class="line"></span><br><span class="line">    add #include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line">commit eb6436b59b7a0624f3ec5e5469ac36b37b5211e7</span><br><span class="line">Author: spitfire-sidra &lt;spitfire.sidra@gmail.com&gt;</span><br><span class="line">Date: Mon Sep 22 15:43:55 2014 +0800</span><br><span class="line"></span><br><span class="line">    add feature_b.c</span><br><span class="line"></span><br><span class="line">commit 38844ba14312c642dcd0f72baf031de0c50ad736</span><br><span class="line">Author: spitfire-sidra &lt;spitfire.sidra@gmail.com&gt;</span><br><span class="line">Date: Mon Sep 22 15:41:04 2014 +0800</span><br><span class="line"></span><br><span class="line">    add HelloWorld.c</span><br><span class="line"></span><br><span class="line">commit 3908e6bc1007f12566fdb5a0fb43f4055560b880</span><br><span class="line">Author: spitfire-sidra &lt;spitfire.sidra@gmail.com&gt;</span><br><span class="line">Date: Mon Sep 22 15:40:42 2014 +0800</span><br><span class="line"></span><br><span class="line">    initial commit</span><br></pre></td></tr></table></figure>

<p>以上就是最简单的rebase过程。</p>
<p>但是在这过程中，有些人可能产生了几个疑问——「为什么先rebase feature-a再rebase feature-b后，会是feature-a的日志在最上方呢？」</p>
<p>这是由于rebase会先找出与newbase之间最近的一个共同base，然后先保留HEAD所在分支(也就是当前分支)从共同base开始的所有变更，接着从共同base开始，将newbase的变更重新套用到HEAD的所在分支后，再将方才所保留的当前分支变更一个一个套用进来，也因此feature-a会是最后的一个commit。</p>
<p>我们一样以图示进行说明。下图<strong>After rebase feature-a</strong>是rebase feature-a之后的样子，可以看到rebase feature-a之后develop与feature-b的共同base是commit 38844b，因此如果要再rebase feature-b的话，commit 15bf9c会先被暂存起来，先进行rebase feature-b之后，再将刚刚暂存的commit 38844b重演一次，所以在图<strong>After rebase feature-b</strong>中feature-a的commit ID就从338844b变成07ef0b，这就是rebase的过程了。</p>
<p><img src="/p/b1718ace/3.png" alt="1"></p>
<p>After rebase feature-a</p>
<p><img src="/p/b1718ace/4.png" alt="1"></p>
<p>After rebase feature-b</p>
<p>问题又来了，刚刚学的rebase会将整个分支都接上去，有时候我们不需要整个分支都接上去，只要接到分支上的某个提交的点即可，这种情况下可以使用rebase – onto进行。</p>
<p>假设只需要接到feature-b的commit eb6436时，就可以用以下指令进行rebase：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git rebase feature-b --onto eb6436</span><br></pre></td></tr></table></figure>

<p>又或者，想要把我们现在的分支整个接到某个分支点上面时，可以选择另一种用法：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git rebase --onto &lt;new base-commit&gt; &lt;current base-commit&gt;</span><br></pre></td></tr></table></figure>

<p>例如，我们在feature-b分支上时，想把整个分支接到commit 3908e6 (initial commit)时，可以输入以下指令：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git co feature-b #先切换到feature-b</span><br><span class="line">$ git rebase --onto 3908e6 38844b</span><br></pre></td></tr></table></figure>

<p>下面2 张图就是执行上述指令的前后对照。</p>
<p><img src="/p/b1718ace/5.png" alt="1"></p>
<p>before rebase –onto 3908e6 38844b</p>
<p><img src="/p/b1718ace/6.png" alt="1"></p>
<p>after rebase –onto 3908e6 38844b</p>
<h2 id="Rebase-进阶互动模式"><a href="#Rebase-进阶互动模式" class="headerlink" title="Rebase -进阶互动模式"></a>Rebase -进阶互动模式</h2><p>Rebase的互动模式十分强大，可以允许我们交换提交的次序、修改提交内容、合并提交内容，甚至将一个提交拆解成多个提交。</p>
<p>要进入互动模式的基本指令如下，base commit可以是分支上的任意一点：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git rebase -i &lt;base commit&gt;</span><br></pre></td></tr></table></figure>

<p>例如，我们想利用互动模式将feature-b上的提交做一些整理时，就可以用以下指令进入互动模式：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git rebase -i 38844b</span><br></pre></td></tr></table></figure>

<p>上述指令的意思就是我们希望将feature-b从commit 38844b之后的所有提交(<code>不含commit 38844b</code>)进行整理。</p>
<p>接着就会出现类似以下的讯息：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">pick 1011f14 add feature_b.c</span><br><span class="line">pick d26076a add #include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line"># Rebase 38844ba..d26076a onto 38844ba</span><br><span class="line">#</span><br><span class="line"># Commands:</span><br><span class="line"># p, pick = use commit</span><br><span class="line"># r, reword = use commit, but edit the commit message</span><br><span class="line"># e, edit = use commit, but stop for amending</span><br><span class="line"># s, squash = use commit, but meld into previous commit</span><br><span class="line"># f, fixup = like &quot;squash&quot;, but discard this commit&apos;s log message</span><br><span class="line"># x, exec = run command (the rest of the line) using shell</span><br><span class="line">#</span><br><span class="line"># These lines can be re-ordered; they are executed from top to bottom.</span><br><span class="line">#</span><br><span class="line"># If you remove a line here THAT COMMIT WILL BE LOST.</span><br><span class="line">#</span><br><span class="line"># However, if you remove everything, the rebase will be aborted.</span><br><span class="line">#</span><br><span class="line"># Note that empty commits are commented out</span><br></pre></td></tr></table></figure>

<p>在进一步操作前，我们必须对讯息上的几个指令(commands)进行说明：</p>
<table>
<thead>
<tr>
<th>pick:</th>
<th>保留此提交</th>
</tr>
</thead>
<tbody><tr>
<td>reword:</td>
<td>修改提交的讯息(只改提交讯息)</td>
</tr>
<tr>
<td>edit:</td>
<td>保留此提交，但是需要做一些修改(例如在程式里面多加些注解)</td>
</tr>
<tr>
<td>squash:</td>
<td>保留此提交，但是将上面的提交一并并入此提交，此动作会显示提交讯息供人编辑</td>
</tr>
<tr>
<td>fixup:</td>
<td>与squash相似，但是此提交的提交讯息会被上面的提交讯息取代</td>
</tr>
<tr>
<td>exec:</td>
<td>执行shell指令，例如<strong>exec make test</strong>进行一些测试，可以随意穿插在提交点之间</td>
</tr>
</tbody></table>
<h3 id="变换顺序"><a href="#变换顺序" class="headerlink" title="变换顺序"></a>变换顺序</h3><p>接下来，简单示范变换提交的顺序，此处我们想把提交的顺序变成先commit 1011f14再来才是commit d26076a，我们只要简单将上述的rebase讯息换成如下的讯息，也就是两行互换即可，就能够变换顺序了！</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"># 此处调换次序即可</span><br><span class="line">pick d26076a add #include &lt;stdio.h&gt;</span><br><span class="line">pick 1011f14 add feature_b.c</span><br><span class="line"></span><br><span class="line"># Rebase 38844ba..d26076a onto 38844ba</span><br><span class="line">#</span><br><span class="line"># Commands:</span><br><span class="line"># p, pick = use commit</span><br><span class="line"># r, reword = use commit, but edit the commit message</span><br><span class="line"># e, edit = use commit, but stop for amending</span><br><span class="line"># s, squash = use commit, but meld into previous commit</span><br><span class="line"># f, fixup = like &quot;squash&quot;, but discard this commit&apos;s log message</span><br><span class="line"># x, exec = run command (the rest of the line) using shell</span><br><span class="line">#</span><br><span class="line"># These lines can be re-ordered; they are executed from top to bottom.</span><br><span class="line">#</span><br><span class="line"># If you remove a line here THAT COMMIT WILL BE LOST.</span><br><span class="line">#</span><br><span class="line"># However, if you remove everything, the rebase will be aborted.</span><br><span class="line">#</span><br><span class="line"># Note that empty commits are commented out</span><br></pre></td></tr></table></figure>

<h3 id="修改提交内容"><a href="#修改提交内容" class="headerlink" title="修改提交内容"></a>修改提交内容</h3><p>有些时候，我们提交之后，不免会注解忘了加或是程式内还有测试的code忘记清掉。这时候除了用git reset –soft HEAD^之外，也可以用rebase编辑那些需要修正的提交。</p>
<p>例如，我们希望用rebase在commit 1011f14中添加几个提交，就可以将pick改成edit进入编辑状态。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">pick 1011f14 add feature_b.c</span><br><span class="line">edit d26076a add #include &lt;stdio.h&gt;</span><br><span class="line"></span><br><span class="line"># Rebase 38844ba..d26076a onto 38844ba</span><br><span class="line">#</span><br><span class="line"># Commands:</span><br><span class="line"># p, pick = use commit</span><br><span class="line"># r, reword = use commit, but edit the commit message</span><br><span class="line"># e, edit = use commit, but stop for amending</span><br><span class="line"># s, squash = use commit, but meld into previous commit</span><br><span class="line"># f, fixup = like &quot;squash&quot;, but discard this commit&apos;s log message</span><br><span class="line"># x, exec = run command (the rest of the line) using shell</span><br><span class="line">#</span><br><span class="line"># These lines can be re-ordered; they are executed from top to bottom.</span><br><span class="line">#</span><br><span class="line"># If you remove a line here THAT COMMIT WILL BE LOST.</span><br><span class="line">#</span><br><span class="line"># However, if you remove everything, the rebase will be aborted.</span><br><span class="line">#</span><br><span class="line"># Note that empty commits are commented out</span><br></pre></td></tr></table></figure>

<p>接下来，如果用git status就可以看到我们正在rebase的讯息：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$ git status</span><br><span class="line">rebase in progress; onto 38844ba</span><br><span class="line">You are currently editing a commit while rebasing branch &apos;Feature-B&apos; on &apos;38844ba&apos;.</span><br><span class="line">  (use &quot;git commit --amend&quot; to amend the current commit)</span><br><span class="line">  (use &quot;git rebase --continue&quot; once you are satisfied with your changes)</span><br><span class="line"></span><br><span class="line">nothing to commit, working directory clean</span><br></pre></td></tr></table></figure>

<p><strong>如果你只是想修正提交讯息</strong>，就可以用以下指令：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git commit --amend</span><br></pre></td></tr></table></figure>

<p><strong>如果你需要多增加几个提交，直接编辑吧</strong>，接着用git add <file> , git commit -m <message>等一般操作进行。最后再利用以下指令完成rebase：</message></file></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git rebase --continue</span><br></pre></td></tr></table></figure>

<p><strong>又或者，我们现在编辑的提交实在是太大了，可能对程式码审查的人造成困扰，例如同时修正太多个档案，我们希望拆成比较明确的多个提交</strong>，就可以用以下指令回到未提交前的状态：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git reset HEAD^</span><br></pre></td></tr></table></figure>

<p>然后就可以用git status列出这个提交中变更了多少档案，然后依照需求一个一个用git add加进去后提交，多提交个几次，就等于是将一个提交拆成多个提交啰！不过别忘了，要用以下指令结束rebase。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git rebase --continue</span><br></pre></td></tr></table></figure>

<p>以上就是rebase的几个简单说明与操作。</p>
<p>至于squash , fixup以及exec就留给各位去体验了！</p>
<h2 id="Rebase出现问题时的处理方法"><a href="#Rebase出现问题时的处理方法" class="headerlink" title="Rebase出现问题时的处理方法"></a>Rebase出现问题时的处理方法</h2><p>Rebase与merge一样都可能会产生<strong>conflict</strong>，这时候除了修正<strong>conflict</strong>之后再用git add <file> , git rebase –continue完成rebase之外，也可以用git rebase –abort直接放弃rebase。</file></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git rebase (--continue | --abort | --skip)</span><br></pre></td></tr></table></figure>

<p>此外，对于rebase使用不慎时，我们会希望能够直接回复到rebase之前的状态，以下就是几个指令可以用来回复到rebase之前的状态。参考自<a href="http://stackoverflow.com/questions/134882/undoing-a-git-rebase" target="_blank" rel="noopener">StackOverFlow</a>。</p>
<p>回复方法1 ：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># 最简单的用法</span><br><span class="line">$ git reset --hard ORIG_HEAD</span><br></pre></td></tr></table></figure>

<p>回复方法2 ：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"># rebase 之前先上tag</span><br><span class="line">$ git tag BACKUP</span><br><span class="line">$ ... # rebase 过程</span><br><span class="line">$ ... # rebase 过程</span><br><span class="line">$ git reset --hard BACKUP # 失败的话可以直接回复到tag BACKUP</span><br></pre></td></tr></table></figure>

<p>回复方法3 ：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git reflog # 寻找要回复的HEAD ，以下假设是HEAD@&#123;3&#125;</span><br><span class="line">$ git reset --hard HEAD@&#123;3&#125; # 回复</span><br></pre></td></tr></table></figure>
    </div>

    
    
    
        
      
        <div id="reward-container">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作, 一个耿直的笑脸~</div>
  <button id="reward-button" disable="enable" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
        
      
      <div style="display: inline-block">
        <img src="/images/wechatpay.jpg" alt="Levon 微信支付">
        <p>微信支付</p>
      </div>
        
      
      <div style="display: inline-block">
        <img src="/images/alipay.jpg" alt="Levon 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/tags/linux/" rel="tag"># linux</a>
            
              <a href="/tags/git/" rel="tag"># git</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
                <a href="/p/df25a0d8.html" rel="next" title="esayrsa生成ssl证书">
                  <i class="fa fa-chevron-left"></i> esayrsa生成ssl证书
                </a>
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
                <a href="/p/4c5612cb.html" rel="prev" title="golang闭包的坑">
                  golang闭包的坑 <i class="fa fa-chevron-right"></i>
                </a>
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          
    
    
  <div class="comments" id="comments">
    <div id="disqus_thread">
      <noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>
    </div>
  </div>
  
  

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">
        
        
        
        
      

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#什么是rebase"><span class="nav-number">1.</span> <span class="nav-text">什么是rebase</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Rebase-基础用法"><span class="nav-number">2.</span> <span class="nav-text">Rebase -基础用法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Rebase-进阶互动模式"><span class="nav-number">3.</span> <span class="nav-text">Rebase -进阶互动模式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#变换顺序"><span class="nav-number">3.1.</span> <span class="nav-text">变换顺序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#修改提交内容"><span class="nav-number">3.2.</span> <span class="nav-text">修改提交内容</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Rebase出现问题时的处理方法"><span class="nav-number">4.</span> <span class="nav-text">Rebase出现问题时的处理方法</span></a></li></ol></div>
        
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
      src="/images/avatar.jpg"
      alt="Levon">
  <p class="site-author-name" itemprop="name">Levon</p>
  <div class="site-description" itemprop="description">Never Give Up</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">127</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-categories">
        
          
            <a href="/categories/">
          
        
        <span class="site-state-item-count">34</span>
        <span class="site-state-item-name">分类</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
          
            <a href="/tags/">
          
        
        <span class="site-state-item-count">76</span>
        <span class="site-state-item-name">标签</span>
        </a>
      </div>
    
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://github.com/unix2dos" title="GitHub &rarr; https://github.com/unix2dos" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="mailto:levonfly@gmail.com" title="E-Mail &rarr; mailto:levonfly@gmail.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://twitter.com/levonfly" title="Twitter &rarr; https://twitter.com/levonfly" rel="noopener" target="_blank"><i class="fa fa-fw fa-twitter"></i>Twitter</a>
      </span>
    
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://weibo.com/l6241425" title="Weibo &rarr; https://weibo.com/l6241425" rel="noopener" target="_blank"><i class="fa fa-fw fa-weibo"></i>Weibo</a>
      </span>
    
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Levon</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    <span title="站点总字数">507k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">7:41</span>
</div>

        












        
      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js?v=3.1.0"></script>
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="/js/utils.js?v=7.4.0"></script><script src="/js/motion.js?v=7.4.0"></script>
<script src="/js/schemes/pisces.js?v=7.4.0"></script>

<script src="/js/next-boot.js?v=7.4.0"></script>



  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>








  <script src="/js/local-search.js?v=7.4.0"></script>














  

  

  

<script>
  function loadCount() {
    var d = document, s = d.createElement('script');
    s.src = 'https://levonfly.disqus.com/count.js';
    s.id = 'dsq-count-scr';
    (d.head || d.body).appendChild(s);
  }
  // defer loading until the whole page loading is completed
  window.addEventListener('load', loadCount, false);
</script>
<script>
  function disqus_config() {
    this.page.url = "https://unix2dos.github.io/p/b1718ace.html";
    this.page.identifier = "p/b1718ace.html";
    this.page.title = '不会rebase就等于没学过Git';};
  function loadComments() {
    if (window.DISQUS) {
      DISQUS.reset({
        reload: true,
        config: disqus_config
      });
    } else {
      var d = document, s = d.createElement('script');
      s.src = 'https://levonfly.disqus.com/embed.js';
      s.setAttribute('data-timestamp', '' + +new Date());
      (d.head || d.body).appendChild(s);
    }
  }
    window.addEventListener('load', loadComments, false);
  
</script>

</body>
</html>
